package com.example.base.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.base.activity.LoginActivity;
import com.example.base.bean.Loginone;
import com.example.base.R;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;

public class RegisterFragment extends Fragment {
  private LinearLayout linearLayout;
  private EditText name;
  private EditText password;
  private EditText repassword;
  private Button register;
  private String username;
  private String pwd;
  private String rpwd;
  private Context context;

  public RegisterFragment() {
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View inflate = inflater.inflate(R.layout.fragment_register, container, false);
    initView(inflate);
    initListener();
    return inflate;
  }

  private void initView(View inflate) {
    linearLayout = inflate.findViewById(R.id.linear_login);
    name = inflate.findViewById(R.id.ed_name_reg);
    password = inflate.findViewById(R.id.ed_password_reg);
    repassword = inflate.findViewById(R.id.ed_password_reg2);
    register = inflate.findViewById(R.id.btn_reg);
    context = getContext();
  }

  private void initListener() {
    linearLayout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        final LoginActivity loginActivity = (LoginActivity) getActivity();
        loginActivity.chageViewPager(0);
      }
    });
    register.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
            username = name.getText().toString();
            pwd = password.getText().toString();
            rpwd = repassword.getText().toString();
            if (!pwd.equals(rpwd)) {
              Toast.makeText(context, "两次输入密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
            } else {
              OkHttpClient client = new OkHttpClient();
              RequestBody body = new FormBody.Builder()
                  .add("username", username)
                  .add("password", pwd)
                  .add("repassword", rpwd)
                  .build();
              Request request = new Request.Builder()
                  .url("https://www.wanandroid.com/user/register")
                  .post(body)
                  .build();
              Call call = client.newCall(request);
              call.enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                  if (response.isSuccessful()) {
                    String data = response.body().string();
                    Gson gson = new Gson();
                    Loginone loginone = gson.fromJson(data, Loginone.class);
                    Log.i(TAG, "onResponse: ");
                    if (loginone.getErrorCode() == 0) {
                      getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                          Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                          Intent intent = new Intent();
                          intent.putExtra("result", loginone.getData().getUsername());
                          getActivity().setResult(1, intent);
                          //关闭Activity
                          getActivity().finish();
                        }
                      });
                    } else {
                      getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                          Toast.makeText(context, loginone.getErrorMsg(), Toast.LENGTH_SHORT).show();
                        }
                      });
                    }


                  }

                }
              });

            }
          }
        });


      }
    });
  }
}